package com.smile.menu;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

import com.badlogic.gdx.math.Vector2;
import com.huw.debuger.FileIO;
import com.light.buttons.Button.ButtonState;
import com.light.gamecomponents.IGameComponents;
import com.light.roller.BigMenu;
import com.light.roller.Direction;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class MenuButton implements IGameComponents ,IUpdateHandler,SensorEventListener{

	private Vector2 m_spritePosition;
	private boolean isLocked = true;
	public Vector2 getSpritePosition() { return m_spritePosition; }
	public int centerX;
    public int centerY;
    int StageNum=1;
    
    private String lockedPicText = "lockedSpot";
    private String openPicText = "StartButton";
    
	protected TextureRegion m_textureRegion;

	protected BaseGameActivity m_game;
	protected Sprite m_sprite;
	protected Direction m_direction;
	protected ButtonState m_state = ButtonState.CONTROLABLE;
	protected LoopEntityModifier m_nowModifiler;
	public void setState(ButtonState state) { m_state = state; }
    
    
    private MenuSpotManager m_menuManager;
    private BigMenu m_menu;
    
    public void setStageNum(int stage)
    {
    	StageNum = stage;
    }
    
	public MenuButton(BigMenu menu, int positionX, int positionY,MenuSpotManager menuManager) {
		centerX = positionX;
		centerY = positionY;
		m_menu = menu;
		m_menuManager = menuManager;
	}

	@Override
	public void onLoadResources() {
		if(isLocked)
		{
			ResourceManager.onLoadResources(lockedPicText, m_menu);
			this.m_textureRegion = ResourceManager.getTextureRegion(lockedPicText);
		}else{
			ResourceManager.onLoadResources(openPicText, m_menu);
			this.m_textureRegion = ResourceManager.getTextureRegion(openPicText);
		}
	}
	@Override
	public void unLoadResources() {
		this.m_textureRegion = null;
		ResourceManager.unLoadResources(openPicText, m_menu);
	}
	
	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) {
		
		m_sprite = new Sprite(centerX, centerY, this.m_textureRegion)
		{
			@Override
        	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
        	{
				if (isLocked == false)
				{
					if (pSceneTouchEvent.isActionDown())
					{
						m_menuManager.setDestination(MenuButton.this);		
					}
					return true;
				}

				return true;
        	}
		};

		
		m_sprite.setScaleCenter(0, 0);
		//m_sprite.setScaleCenter(centerX, centerY);
		m_sprite.setPosition(centerX, centerY);
		centerX += BigMenu.divX;
		centerY += BigMenu.divY;
		scene.attachChild(m_sprite);   
		scene.registerTouchArea(m_sprite);
		
	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		scene.unregisterTouchArea(m_sprite);
		scene.detachChild(m_sprite);

		m_sprite = null;
	}
	
	public void setLocked(boolean locked)
	{
		isLocked = locked;
	}

	@Override
	public void onLoadEngine() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		
	}
	
	public void setLockedPicText(String picText)
	{
		this.lockedPicText = picText;
	}
	public void setOpenPicText(String picText)
	{
		this.openPicText = picText;
	}

}
